<?php
namespace app\api\controller\deerhome\shop;

use app\api\controller\deerhome\shop\BaseAuth;
use think\Db;
use think\Validate;
use app\admin\model\deerhome\Shop as ShopModel;
use app\admin\model\deerhome\Order as OrderModel;
use app\admin\model\deerhome\OrderLog as OrderLogModel;

class Order extends BaseAuth
{
    //根据核销码和门店id查找订单
    public function get_by_code()
    {
        $shopId=$this->request->param("shopId",0,"intval");
        $code=$this->request->param("code",'',"trim");
        $where=[];
        $where["deerhome_shop_id"]=$shopId;
        $where["hx_code"]=$code;
        $where["order_way"]=2;
        $where["status"]=3;
        $order=OrderModel::where($where)->find();
        if(!$order){
            $this->err("核销码错误或待核销的订单不存在",1);
        }
        $this->ok($order);
    }
    public function done()
    {
        $shopId=$this->request->param("shopId",0,"intval");
        $sn=$this->request->param("sn",'',"trim");
        $code=$this->request->param("code",'',"trim");
        $imgs=$this->request->param("imgs/a",[]);
        $note=$this->request->param("note","",'trim');
        $where=[];
        $where["deerhome_shop_id"]=$shopId;
        $where["sn"]=$sn;
        $where["hx_code"]=$code;
        $where["status"]=3;
        $where["order_way"]=2;
       
        $order=OrderModel::where($where)->find();
        if(!$order){
            $this->err("核销码错误或待核销的订单不存在",1);
        }
        Db::startTrans();
        try{
            $order->status=5;
            $order->worker_apply_time=\date("Y-m-d H:i:s");
            if(count($imgs)>0){
                $order->worker_imgs=implode(",",$imgs);
            }
            $order->worker_note=$note;
            $order->save();
            OrderLogModel::insert([
                "order_id"=>$order->id
                ,"ac_user"=>'门店:'.$this->_user['uname']
                ,"note"=>"订单已核销成功,服务完成！"
                ,"add_time"=>\date("Y-m-d H:i:s")
            ]);
            
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this->err($e->getMessage());
        }
        $this->ok();
    }
    public function rows()
    {
        $id=$this->request->param("id",0,"intval");
        $status=$this->request->param("status",0,"intval");
        $key=$this->request->param("key","","trim");
        $day=$this->request->param("day","","trim");
        $page=$this->request->param("page",1,"intval");
        $where=[];
        $where["order.order_way"]=2;
        $where["order.status"]=[">=",3];
        $where["deerhome_shop_id"]=$id;
        if($status>=3){
            $where["order.status"]=$status;
        }
        if($day){
            $where["order.date_day"]=$day;
        }
        if($key){
            $where["order.sn|order.user_name|order.user_phone"]=["like","%$key%"];
        }
        $out=OrderModel::where($where)->with([
            'items'=>function($query){
                $query->field('face_image');
            }
        ])->order('order.date_day desc,order.date_time desc')->paginate(10);
        $this->ok($out);
    }
    public function index()
    {
        $id=$this->request->param("id",0,"intval");
        $out=[];
        $out['date']=[];
        $date_before=\date("Y-m-d",strtotime("-2 day"));
        $date_after=\date("Y-m-d",strtotime("+10 day"));
        $orderNumArr=OrderModel::where("order_way",2)->where("status",">=",3)->where("deerhome_shop_id",$id)->where("date_day",">=",$date_before)->where("date_day","<=",$date_after)->field("date_day,count(id) as numCount")->group("date_day")->select();
        $orderNumArrFormat=[];
        foreach($orderNumArr as $v){
            $orderNumArrFormat[$v['date_day']]=$v['numCount'];
        }
        for($i=-2;$i<=10;$i++){
            $date=\date("Y-m-d",strtotime("+$i day"));
            $num=0;
            if(isset($orderNumArrFormat[$date])){
                $num=$orderNumArrFormat[$date];
            }
            array_push($out['date'],[
                "date"=>$date
                ,"day"=>\date("m/d",strtotime($date))
                ,"week"=>$date==date('Y-m-d')?'今天':$this->getWeek($date)
                ,"num"=>$num
            ]);
        }
        $out['nav']=[];
        $navs=(new OrderModel())->getStatusList();
        foreach($navs as $k=>$v){
            if($k<=2){continue;}
            array_push($out['nav'],[
                "name"=>$v
                ,"id"=>$k
            ]);
        }
        $this->ok($out);
    }
}
